﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dev.Control;
using Bill.BasDLL.biz;
using TT.Common;
using Bill.BasDLL.ety;

namespace Bill.BasForm.sfcorpBill
{
    public partial class frmBillPrice : BaseFormDev
    {
        bizBillPrice bzBillPrice;
        TT.Module.sysLogin.etyBaeSystemParamter basSysPara;
        TT.Module.GetSqliBaseData sqliBaseData;

        public frmBillPrice(TT.Module.sysLogin.etyBaeSystemParamter basSysPara)
        {
            this.basSysPara = basSysPara;
            this.sqliBaseData = new TT.Module.GetSqliBaseData(basSysPara.SqliBaseHelp);
            InitializeComponent();
        }

        private string formSta;
        public string FormSta
        {
            set
            {
                formSta = value;
                switch (formSta)
                {
                    case "A":
                        if (!string.IsNullOrEmpty(cboCustomer.Text) 
                            && !string.IsNullOrEmpty(txtPrice.Text)
                            && TT.HelpInfo.MsgBox.ShowQuestionBox("是否清空控件值？"))
                        {
                            ClearForm();
                            ClearForm();
                        }
                        else
                            cboCustomer.SelectedIndex = -1;
                        lblBar.Caption = "新建状态";
                        break;
                    case "E":
                        lblBar.Caption = "编辑状态";
                        break;
                    default:
                        break;
                }
            }
            get
            {
                return formSta;
            }
        }
        #region 事件

        //显示行号
        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }
        private void frmBillPrice_Load(object sender, EventArgs e)
        {
            bzBillPrice = new bizBillPrice(basSysPara);

            #region 下拉数据
            IList<Bill.BasDLL.ety.etyCustomer> ilCust = dataProcessor.CopyIList<Bill.BasDLL.ety.etyCustomer>(sqliBaseData.GetBaseData<Bill.BasDLL.ety.etyCustomer>(basSysPara.PORT_CODE, "CBO_C_CUSTOMER"));
            ilCust.Insert(0, new BasDLL.ety.etyCustomer() { CUSTOMER_ID = "***", CUSTOMER_NAM = "***" });
            cboCustomer.DataSource = ilCust;

            IList<TT.ORM.etyCodeRef> ilItem = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_C_BILL_CODE"));
            cboItem.DataSource = ilItem;

            IList<TT.ORM.etyCodeRef> ilPort = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_C_PORT"));
            ilPort.Insert(0, new TT.ORM.etyCodeRef() { CODE = "***", CODE_DESC = "***" });
            cboPortId.DataSource = ilPort;

            IList<TT.ORM.etyCodeRef> ilIE = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_C_I_E"));
            ilIE.Insert(0, new TT.ORM.etyCodeRef() { CODE = "***", CODE_DESC = "***" });
            cboIeId.DataSource = ilIE;

            IList<TT.ORM.etyCodeRef> ilCntrCopr = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_C_CNTR_CORP"));
            ilCntrCopr.Insert(0, new TT.ORM.etyCodeRef() { CODE = "***", CODE_DESC = "***" });
            cboCntrCoprCod.DataSource = ilCntrCopr;

            IList<TT.ORM.etyCodeRef> ilFeeWay = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_C_FEE_WAY"));
            ilFeeWay.Insert(0, new TT.ORM.etyCodeRef() { CODE = "***", CODE_DESC = "***" });
            cboFeeWayId.DataSource = ilFeeWay;

            IList<TT.ORM.etyCodeRef> ilWorkWay = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_C_WORK_WAY"));
            ilWorkWay.Insert(0, new TT.ORM.etyCodeRef() { CODE = "***", CODE_DESC = "***" });
            cboWorkWayId.DataSource = ilWorkWay;

            IList<TT.ORM.etyCodeRef> ilCntrSize = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_C_CNTR_SIZ"));
            ilCntrSize.Insert(0, new TT.ORM.etyCodeRef() { CODE = "***", CODE_DESC = "***" });
            cboCntr_Siz_Cod.DataSource = ilCntrSize;

            IList<TT.ORM.etyCodeRef> ilCntrType = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_C_CNTR_TYP"));
            ilCntrType.Insert(0, new TT.ORM.etyCodeRef() { CODE = "***", CODE_DESC = "***" });
            cboCntr_Typ_Cod.DataSource = ilCntrType;

            IList<TT.ORM.etyCodeRef> ilWeight = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_C_SYS_YN"));
            ilWeight.Insert(0, new TT.ORM.etyCodeRef() { CODE = "***", CODE_DESC = "***" });
            cboWeight.DataSource = ilWeight;

            IList<TT.ORM.etyCodeRef> ilMethod = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_C_PER_METHOD"));
            cboPerMeth.DataSource = ilMethod;
            
            IList<TT.ORM.etyCodeRef> ilUnit = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_C_UNIT_REF"));
            cboUnit.DataSource = ilUnit;

            IList<TT.ORM.etyCodeRef> ilForeignVvd = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_C_FOREIGN_VVD"));
            ilForeignVvd.Insert(0, new TT.ORM.etyCodeRef() { CODE = "***", CODE_DESC = "***" });
            cboForeignVvd.DataSource = ilForeignVvd;

            IList<TT.ORM.etyCodeRef> ilCashFlag = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(sqliBaseData.GetBaseData(basSysPara.PORT_CODE, "CBO_CASH_FLAG"));
            cboCASH_FLAG.DataSource = ilCashFlag;
            
            #endregion

            #region 表格数据
            ggcPrice.GridView = this.gridView1;
            ggcPrice.GridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.Click;
            //string sMapingCols = "CUSTOMER,SVC_TYPE,ITEM,CHRG_FLT1,CHRG_FLT2,CHRG_FLT3,CHRG_FLT4,CHRG_FLT5,CHRG_FLT6,CHRG_FLT7,CHRG_FLT8,METHOD,PRICE";
            //string sMapingHeaderTxt = "用户,服务类型,收费代码,码头,货代公司,进出口,结算方式,作业方式,是否过磅,每方法,单价";
            string sMapingCols = "CUSTOMER,CUSTOMER_NAME,ITEM,ITEM_DESC,CHRG_FLT1,CHRG_DESC1,CHRG_FLT2,CHRG_DESC2,CHRG_FLT3,CHRG_DESC3,CHRG_FLT9,CHRG_DESC9,CHRG_FLT4,CHRG_DESC4,CASH_FLAG,CHRG_FLT5,CHRG_DESC5,CHRG_FLT6,CHRG_FLT7,CHRG_FLT8,UNIT,METHOD,PRICE";
            string sMapingHeaderTxt = "客户代码,客户名称,收费代码,费用描述,码头,码头名称,货代公司,货代名称,进出口,进出口描述,内外贸,内外贸描述,结算方式,结算方式描述,强制现金,作业方式,作业方式描述,是否过磅,尺寸,箱型,单位,每方法,单价";
            ggcPrice.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt);
            this.winUtil.LoadGridLayout(ggcPrice);
            ggcPrice.GridView.OptionsView.ShowGroupPanel = true;

            #endregion

            #region 树数据
            IList<TT.ORM.etyCodeRef> treeItem = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(ilItem);
            treeItem.Insert(0, new TT.ORM.etyCodeRef() { CODE = "***", CODE_DESC = "***" });
            tvBillCode.DataSource = treeItem;
            #endregion

            cboCustomer.SelectedIndex = -1;
            txtPrice.Text = string.Empty;
            FormSta = "A";
            bar3.Text = "状态栏";

        }
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshDate();
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NewData();
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveDate();
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteDate();
        }

        private void btnSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView1.OptionsFind.AlwaysVisible = !this.gridView1.OptionsFind.AlwaysVisible;
        }

        private void btnExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            F12();
        }
        private void ggcBillPrice_Click(object sender, EventArgs e)
        {
            etyBillPrice ey = srcBinding.Current as etyBillPrice;
            if (ey == null)
                return;
            TT.Common.formProcess.SetFormEntity(ey, cGroupControl1);
            cboCustomer.GetCodeText();
            cboCntr_Siz_Cod.GetCodeText();
            //cboCntr_Typ_Cod.GetCodeText();
            cboCntrCoprCod.GetCodeText();
            cboFeeWayId.GetCodeText();
            cboIeId.GetCodeText();
            cboItem.GetCodeText();
            cboUnit.GetCodeText();
            cboPerMeth.GetCodeText();
            cboPortId.GetCodeText();
            cboWeight.GetCodeText();
            cboWorkWayId.GetCodeText();
            cboForeignVvd.GetCodeText();
            cboCASH_FLAG.GetCodeText();
            FormSta = "E";
        }
        #endregion

        public override Dev.Control.etyQuickKey RefreshDate()
        {
            srcBinding.DataSource = bzBillPrice.GetBillPrice("", "", tvBillCode.SelectedNodeText.Split(':')[0]);
            ggcPrice.DataSource = srcBinding;
            ggcPrice.GridView.BestFitColumns();
            return base.RefreshDate();
        }

        public override etyQuickKey NewData()
        {
            FormSta = "A";
            return base.NewData();
        }

        public override etyQuickKey DeleteDate()
        {
            bool success=false;
            IList<etyBillPrice> ilPrice = new List<etyBillPrice>();
            List<int> selectedRows;
            //筛选选中的行，非数据行的不要。
            selectedRows = ggcPrice.GridView.GetSelectedRows().Where((t) => t >= 0).ToList();
            foreach (int i in selectedRows)
            {
                etyBillPrice dr = ggcPrice.GridView.GetRow(i) as etyBillPrice;
                ilPrice.Add(dr);
            }
            if (ilPrice.Count <= 0) return base.DeleteDate(); 
            if (TT.HelpInfo.MsgBox.ShowQuestionBox(string.Format("是否删除选中的价格表？", new string[] { })))
            {
                foreach(etyBillPrice ey in ilPrice)
                {
                    success = bzBillPrice.Delete(ey);
                    if(!success)
                    {
                        TT.HelpInfo.MsgBox.ShowSuccessBox("删除失败");
                        return base.DeleteDate();
                    }
                }
                TT.HelpInfo.MsgBox.ShowSuccessBox("删除成功");
                lblBar.Caption = "";
                RefreshDate();
            }
            return base.DeleteDate();
        }

        public override etyQuickKey SaveDate()
        {
            etyBillPrice ey = new etyBillPrice();
            TT.Common.formProcess.GetFormEntity(ey, cGroupControl1);
            ey.PRICE = ey.PRICE.TrimStart('¥');
            if (check_data(ey))
            {
                bool rst;
                if (FormSta == "A")
                    rst = bzBillPrice.Insert(ey);
                else
                    rst = bzBillPrice.Update(ey);
                if (rst)
                {
                    TT.HelpInfo.MsgBox.ShowSuccessBox("保存成功");
                    RefreshDate();
                }
                else
                {
                    TT.HelpInfo.MsgBox.ShowWarnBox("保存失败");
                }
            }
            return base.SaveDate();
        }

        #region 函数
        private void ClearForm()
        {
            cboCustomer.SelectedIndex = -1;
            cboItem.SelectedIndex = -1;
            //cboSvc_Type.SelectedIndex = -1;
            cboPortId.SelectedIndex = 0;
            cboCntrCoprCod.SelectedIndex = 0;
            cboIeId.SelectedIndex = 0;
            cboFeeWayId.SelectedIndex = 0;
            cboWorkWayId.SelectedIndex = 0;
            cboWeight.SelectedIndex = 0;
            cboCntr_Siz_Cod.SelectedIndex = 0;
            cboCntr_Typ_Cod.SelectedIndex = 0;
            cboForeignVvd.SelectedIndex = 0;
            cboUnit.SelectedIndex = -1;
            cboPerMeth.SelectedIndex = -1;
            txtPrice.Text = "";
        }
        private bool check_data(etyBillPrice ey)
        {
            if (string.IsNullOrEmpty(ey.CUSTOMER.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("用户不能为空");
                cboCustomer.Focus();
                return false;
            }
            //if (string.IsNullOrEmpty(ey.SVC_TYPE.Trim()))
            //{
            //    TT.HelpInfo.MsgBox.ShowWarnBox("服务类型不能为空");
            //    cboSvc_Type.Focus();
            //    return false;
            //}
            if (string.IsNullOrEmpty(ey.ITEM.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("收费代码不能为空");
                cboItem.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(ey.METHOD.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("每方法不能为空");
                cboPerMeth.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(ey.PRICE.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("单价不能为空");
                txtPrice.Focus();
                return false;
            }
            decimal dPrice;
            if (!decimal.TryParse(ey.PRICE.Trim(), out dPrice))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("单价格式错误");
                txtPrice.Focus();
                return false;
            }
            return true;
        }
        #endregion

        private void tvBillCode_SelectionChanged(object sender, EventArgs e)
        {
            RefreshDate();
        }

        private void cboCustomer_SelectedValueChanged(object sender, EventArgs e)
        {
            RefreshDate();
        }

        private void tvBillCode_TextChanged(object sender, EventArgs e)
        {
            RefreshDate();
        }

        private void tvBillCode_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            RefreshDate();
        }

        private void barPriceCopy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            IList<etyBillPrice> ilPrice = new List<etyBillPrice>();
            List<int> selectedRows;
            //筛选选中的行，非数据行的不要。
            selectedRows = ggcPrice.GridView.GetSelectedRows().Where((t) => t >= 0).ToList();
            foreach (int i in selectedRows)
            {
                etyBillPrice dr = ggcPrice.GridView.GetRow(i) as etyBillPrice;
                ilPrice.Add(dr);
            }
            frmBillPriceCopy frm = new frmBillPriceCopy(basSysPara, ilPrice);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                this.RefreshDate();
        }

    }
}
